﻿namespace CatchBall
{
    using System;
    using System.IO;
    using System.Media;
    using System.Threading;

    public class MainCatchBall
    {
        // printing team logo - yay!
        public static void Cyclope()
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(@"                   ///\\\                       ///\\\    ");
            Console.WriteLine(@"                  ///  \\\                     ///  \\\   ");
            Console.WriteLine(@"                 ///    \\\                   ///    \\\  ");
            Console.WriteLine(@"                 \\\     \\\                 ///     ///  ");
            Console.Write(@"                  \\\/    \\\   ");
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.Write(@"_......._   ");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(@"///    \///   ");
            Console.Write(@"                           \\");
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.Write(@"-'.'.'.'.'.'.`-");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(@"//            ");
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine(@"                          .'.'.'.'.'.'.'.'.'.`.           ");
            Console.WriteLine(@"                         /.'.'               '.\          ");
            Console.WriteLine(@"                         |.'    _.--...--._     |         ");
            Console.Write(@"                         \    `._.-.....-._.'   ");
            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.WriteLine(@"/         ");
            Console.Write(@"                         |   ");
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write(@" _..- .-. -.._    ");
            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.WriteLine(@"|          ");
            Console.Write(@"                      .-.'   ");
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write(@" `.  ");
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.Write(@" ((@)) ");
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write(@" .'  ");
            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.WriteLine(@" '.-.       ");
            Console.Write(@"                     ( ^ \   ");
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write(@"   `--.   .-'   ");
            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.WriteLine(@"  / ^ )      ");
            Console.WriteLine(@"                      \  /         .   .       \  /       ");
            Console.WriteLine(@"                      /          .'     '.  .-    \       ");
            Console.WriteLine(@"                     ( _.\    \ (_`-._.-'_)    /._\)      ");
            Console.Write(@"                      `-' \   '");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write(@" .--.         ");
            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.WriteLine(@" / `-'       ");
            Console.Write(@"                          |  / ");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write(@"/ ");
            Console.BackgroundColor = ConsoleColor.White;
            Console.Write(@"  ");
            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write(@" `-._.'\  ");
            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.WriteLine(@" |           ");
            Console.Write(@"                          |   ");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write(@"|        ");
            Console.BackgroundColor = ConsoleColor.White;
            Console.Write(@"  ");
            Console.BackgroundColor = ConsoleColor.Black;
            Console.Write(@" |");
            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.WriteLine(@"   /-.._       ");
            Console.Write(@"                      _..-\   ");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write(@"`.--.______.' ");
            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.WriteLine(@" |            ");
            Console.WriteLine(@"                           \       .....     |            ");
            Console.WriteLine(@"                            `.  .'      `.  /             ");
            Console.WriteLine(@"                              \           .'              ");
            Console.WriteLine(@"                               `-..___..-`                ");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(@"                                \\\   ///                 ");
            Console.WriteLine(@"                                 \\\ ///                  ");
            Console.WriteLine(@"                                  \\V//                   ");
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(@"
                ===    /   \ /    /   |      / \   | \   /              
                  /   |     v    |    |     |   |  | /   \         
                 /     \    |     \   |__/   \ /   |     /               ");

            Thread.Sleep(5000);
            for (var i = 0; i < Console.WindowHeight / 2; i++)
            {
                for (var j = 0; j < Console.WindowWidth - 1; j++)
                {
                    Console.SetCursorPosition(j, i);
                    Console.WriteLine(" ");
                    Console.SetCursorPosition(j, (Console.WindowHeight - 2) - i);
                    Console.WriteLine(" ");
                    Thread.Sleep(1);
                }
            }

            Console.SetCursorPosition((Console.WindowWidth / 2) - 7, Console.WindowHeight / 2);
            Console.Write("P R E S E N T");
            Thread.Sleep(3000);
            Console.Clear();
        }

        /// <summary>
        /// Plays a specified audio file.<br></br>
        /// Catches an exception if the file is missing.
        /// </summary>
        /// <param name="file">File path and name.</param>
        /// <param name="looping">Optional. Whether the sound should be played looping. False by default.</param>
        public static void PlayAudioFile(string file, bool looping = false)
        {
            try
            {
                using (var sound = new SoundPlayer(file))
                {
                    if (looping)
                    {
                        sound.PlayLooping();
                    }
                    else
                    {
                        sound.Play();
                    }
                }
            }
            catch (FileNotFoundException e)
            {
                Console.SetCursorPosition(23, 26);
                Console.WriteLine(e.Message); // NON-requred, just to remove the warning that "e" is not used

                // TODO: log exception to text file
            }
        }

        private static void PlayAgain()
        {
            var game = new Game();
            var scores = new Scores(game.ChosenLevel);
            game.Play(scores);

            var playAgainInput = string.Empty;

            Console.SetCursorPosition(23, 21);
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Write("Play again? (y/n): ");
            while (!(playAgainInput == "y" || playAgainInput == "n"))
            {
                Console.CursorVisible = true; 
                playAgainInput = Console.ReadLine();
                playAgainInput = playAgainInput.Trim();
                Console.CursorVisible = false; 

                if (playAgainInput == "y")
                {
                    Console.Clear();
                    PlayAgain();
                }
                else if (playAgainInput == "n")
                {
                    Environment.Exit(0);
                }
            }

            Console.ResetColor();
        }

        private static void GameField()
        {
            Console.Title = "Catch Ball";
            Console.SetWindowSize(76, 32);
            Console.SetBufferSize(76, 32);
            Console.CursorVisible = false;
            Console.BackgroundColor = ConsoleColor.Black;
        }        

        private static void Main()
        {
            GameField();
            
            Cyclope();
            PlayAgain();
        }
    }
}